﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Autofac.Integration.Mvc;
using Timesheet.Data.Infrastructure;
using Timesheet.Data.Repository;
using Timesheet.Service;
using System.Web.Mvc;
using System.Reflection;

namespace Timesheet.Web
{
    public class IoCContainer
    {

        /// <summary>
        /// Dependency binding using Autofac
        /// </summary>
        /// <returns></returns>
        public IDependencyResolver GetAutofacDependencyResolver()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
            builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(typeof(IUserRepository).Assembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerHttpRequest();
            builder.RegisterAssemblyTypes(typeof(IUserService).Assembly).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces().InstancePerHttpRequest();

            builder.RegisterFilterProvider();
            IContainer container = builder.Build();
            return new AutofacDependencyResolver(container);
        }

      
    }
}
